<template>
    <div class="login">
        <div class="l-box">
            <div class="b-logo">
                <img src="/image/logo-icon.png" alt />
                <h1>Tacomall</h1>
            </div>
            <div class="b-description">
                <p>塔可开源，致力于企业级新零售解决方案</p>
            </div>
            <div class="b-input">
                <div class="i-item">
                    <i-input
                        prefix="ios-contact"
                        size="large"
                        placeholder="请输入账号"
                        style="width: 100%; height: 40px"
                        v-model="form.username"
                    />
                </div>
                <div class="i-item">
                    <i-input
                        type="password"
                        prefix="ios-eye-off"
                        size="large"
                        placeholder="请输入密码"
                        style="width: 100%; height: 40px"
                        v-model="form.passwd"
                    />
                </div>
            </div>
            <div class="b-help">
                <div class="h-left">
                    <i-checkbox v-model="autoLogin">自动登录</i-checkbox>
                </div>
                <div class="h-right">
                    <span>忘记密码</span>
                </div>
            </div>
            <div class="b-btn">
                <Button type="primary" size="large" long @click.native="doLogin">登录</Button>
            </div>
            <div class="b-footer">
                <div class="f-link">
                    <a href="/">官网</a>
                    <a href="/">社区</a>
                    <a href="/">专业版</a>
                </div>
                <div class="f-copyright">
                    <span>Copyright © 2020 tacomall</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import api from '@/api'
import { session } from '@/libs/session'
import { validate } from '@/utils/validate'
import { mapActions } from 'vuex'
export default {
    data() {
        return {
            autoLogin: true,
            form: {
                username: '',
                passwd: ''
            }
        }
    },
    methods: {
        ...mapActions('user', ['getUserInfo']),
        init() {
            this.params = this.$route.query
            this.params['t'] === 'trial' && this.doTrial()
        },
        doTrial() {
            this.$Notice.info({
                title: '欢迎体验TACOMALL',
                duration: 0,
                render: h => {
                    return h('div', [
                        '您正在体验TACOMALL平台版后台，如需获取体验账号，请点击',
                        h('a', { attrs: { href: '//www.codingtalk.cn/article/f0fa618b-36ee-49fc-8dfb-e8f9badec08e', target: '_blank' } }, '获取账号'),
                        '链接获取'
                    ])
                }
            });
        },
        async doLogin() {
            if (!validate.isMobile(this.form.username)) {
                this.$Message.error('账号格式有误')
                return
            }
            if (!validate.isNotEmpty(this.form.passwd)) {
                this.$Message.error('密码格式有误')
                return
            }
            const res = await api.user.login(this.form)
            if (res.status) {
                session.set(res.data)
                this.getUserInfo().then(() => {
                    this.rep('/index')
                })
            } else {
                this.$Message.error('账号密码错误')
            }
        }
    },
    created() {
        this.init()
    }
}
</script>
<style lang="less">
.login {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 100%;
    background: url(/image/login.png) center no-repeat;
    background-size: 100% auto;

    .l-box {
        width: 384px;
        height: 100vh;
        box-sizing: border-box;
        padding: 32px 0;
        margin: 0 auto;

        .b-logo {
            display: flex;
            align-items: center;
            justify-content: center;
            padding-top: 40px;

            img {
                width: 40px;
                height: 40px;
            }

            h1 {
                padding-left: 5px;
            }
        }

        .b-description {
            display: flex;
            align-items: center;
            justify-content: center;
            padding-top: 10px;
            p {
            }
        }

        .b-input {
            padding: 20px 0;

            .i-item {
                &:not(:first-child) {
                    padding-top: 20px;
                }
            }
        }
        .b-help {
            display: flex;
            justify-content: space-between;
        }
        .b-btn {
            padding-top: 32px;
        }
        .b-footer {
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            padding: 48px 0 24px 0;

            .f-link {
                display: flex;
                align-items: center;
                justify-content: center;

                a {
                    color: #515a6e;

                    &:not(:first-child) {
                        padding-left: 30px;
                    }
                }
            }

            .f-copyright {
                display: flex;
                justify-content: center;
                padding-top: 10px;
            }
        }
    }
}
</style>
